<?php
    session_start();
    if(!isset($_SESSION["userName"]) or $_SESSION["userType"] === "admin") {
    	header("location: accesserror.php");
    }
    header("text/html;charset=utf8");
    if(!isset($_POST["idea"]) or !isset($_POST["close"]) or !isset($_POST["id"])) { 
    	header("location: accesserror.php");
    }
	$result = "?result=";
	$idea = trim($_POST["idea"]);
	$close = $_POST["close"];
	$id = $_POST["id"];
	if ($idea === "" && $close === "false") { 
		header("location: probleminfo.php".$result."意见反馈不能为空.&id=".$id);
	}
	$connect = mysql_connect("localhost","root","yxs147789");
	if ($connect) {
	  	mysql_select_db("inspectdb", $connect);
	  	mysql_query("set names 'utf8'");
	  	$success = true;
	  	mysql_query("BEGIN");
	  	if($close === "true" && $idea === "") { 
	  		$success = mysql_query(sprintf("UPDATE problems SET state='已关闭', end_time='%s'", curdate()));
	  	} else {
	  		//处理idea,把','替换为"，"
	  		$idea = str_replace(",", "，", $idea);
	  		$success = mysql_query(sprintf("UPDATE problem_info SET user_idea = CONCAT(user_idea, ',%s') WHERE user_name = '%s' and problem_id = '%s'", $idea, $_SESSION["userName"], $id));
	  	}
	  	mysql_query("END");
	  	if($success === true) { 
	  		mysql_query("COMMII");
	  		$result .= "success";
	  	} else { 
	  		mysql_query("ROLLBACK");
	  		$result .= "操作失败";
	  	}
	}
	else {
		$result .= "连接失败";
	}
	mysql_close($connect);
	header("location: probleminfo.php".$result."&id=".$id);
?>